package com.atguigu.tingshu.account.receiver;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.model.account.UserAccount;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * @author: atguigu
 * @create: 2023-11-15 15:11
 */
@Slf4j
@Component
public class AccountReceiver {

    @Autowired
    private UserAccountService userAccountService;


    /**
     * 监听器：监听指定话题中数据，拉取到消息后执行业务逻辑
     * 所有监听器考虑：1.是否需要做幂等性处理(消息被执行一次跟多次达到结果一样)、 2.是否需要事务管理
     *
     * @param record 监听到话题中消息封装key跟value
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_USER_REGISTER)
    public void initUserAccount(ConsumerRecord<String, String> record) {
        String value = record.value();
        //org.apache.commons.lang3
        if (StringUtils.isNotBlank(value)) {
            log.info("[账户服务]监听到用户注册消息：{}", value);
            userAccountService.initUserAccount(Long.valueOf(value));
        }

    }


}
